<template>
  <div class="app-wrapper">
    <header class="app-header">
      <div class="header-logo">
        <img src="../assets/logo.png" alt="Logo" class="logo-img" v-if="false">
        <h1 class="logo-text">农产品金融销售平台</h1>
      </div>
      <div class="header-nav">
        <TopMenu />
      </div>
      <div class="header-tools">
        <el-tooltip content="切换侧边栏" placement="bottom">
          <div class="tool-item" @click="toggleSidebar">
            <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
          </div>
        </el-tooltip>
        <el-tooltip content="刷新页面" placement="bottom">
          <div class="tool-item" @click="refreshPage">
            <i class="el-icon-refresh"></i>
          </div>
        </el-tooltip>
        <el-tooltip content="全屏显示" placement="bottom">
          <div class="tool-item" @click="toggleFullScreen">
            <i :class="isFullscreen ? 'el-icon-close' : 'el-icon-full-screen'"></i>
          </div>
        </el-tooltip>
        <UserAvatar />
      </div>
    </header>
    
    <div class="app-container">
      <!-- <aside class="app-sidebar" :class="{'is-collapse': isCollapse}">
        <SideMenu :is-collapse="isCollapse" ref="sideMenu" />
      </aside> -->
      
      <main class="app-main">
        <div class="main-header">
          <BreadCrumbs />
        </div>
        <div class="main-content card-shadow">
          <el-scrollbar wrap-class="scrollbar-wrapper">
            <router-view @update:user="updateUser" />
          </el-scrollbar>
        </div>
        <footer class="app-footer">
          <div class="footer-copyright">
            <span>© {{ new Date().getFullYear() }} 农产品金融销售平台 版权所有</span>
          </div>
          <div class="footer-links">
            <a href="javascript:;">使用指南</a>
            <a href="javascript:;">关于我们</a>
            <a href="javascript:;">联系客服</a>
          </div>
        </footer>
      </main>
    </div>
  </div>
</template>

<script>
import SideMenu from '../components/Aside.vue'
import TopMenu from '../components/TopMenu.vue'
import BreadCrumbs from '../components/BreadCrumbs/index.vue'
import UserAvatar from '../components/UserAvatar/index.vue'

export default {
  name: 'Layout',
  components: { SideMenu, TopMenu, BreadCrumbs, UserAvatar },
  data() {
    return {
      userInfo: JSON.parse(localStorage.getItem("backUser") || "{}"),
      isCollapse: false,
      isFullscreen: false
    };
  },
  created() {
    // 判断是否登录
    if(!this.userInfo.id){
      this.$message.warning('请先登录')
      this.$router.push('/login')
    }
    
    // 检查屏幕尺寸，在小屏幕上默认折叠侧边栏
    this.checkScreenSize();
    window.addEventListener('resize', this.checkScreenSize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkScreenSize);
  },
  provide() {
    return {
      userInfo: this.userInfo,
    };
  },
  methods: {
    updateUser(user) {
      this.userInfo = user;
      this.$refs.sideMenu.refreshMenu();
    },
    toggleSidebar() {
      this.isCollapse = !this.isCollapse;
    },
    refreshPage() {
      window.location.reload();
    },
    toggleFullScreen() {
      if (!document.fullscreenElement) {
        document.documentElement.requestFullscreen();
        this.isFullscreen = true;
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen();
          this.isFullscreen = false;
        }
      }
    },
    checkScreenSize() {
      this.isCollapse = window.innerWidth < 992;
    }
  },
  mounted() {
    document.addEventListener('fullscreenchange', () => {
      this.isFullscreen = !!document.fullscreenElement;
    });
  }
}
</script>

<style lang="less">
.app-wrapper {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.app-header {
  height: 60px;
  width: 100%;
  background: linear-gradient(135deg, #4caf50, #2e7d32);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
}

.header-logo {
  display: flex;
  align-items: center;
  min-width: 220px;
}

.logo-img {
  height: 36px;
  margin-right: 10px;
}

.logo-text {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  letter-spacing: 1px;
}

.header-nav {
  flex: 1;
  display: flex;
  justify-content: center;
}

.header-tools {
  display: flex;
  align-items: center;
  gap: 15px;
}

.tool-item {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 50%;
  transition: all 0.3s;
}

.tool-item:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.tool-item i {
  font-size: 20px;
}

.app-container {
  display: flex;
  height: calc(100vh - 60px);
  width: 100%;
  overflow: hidden;
}

.app-sidebar {
  width: 220px;
  height: 100%;
  background-color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: width 0.3s;
  overflow: hidden;
  z-index: 999;
}

.app-sidebar.is-collapse {
  width: 64px;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 15px;
  overflow: hidden;
  position: relative;
}

.main-header {
  height: 50px;
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.main-content {
  flex: 1;
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  max-height: calc(100vh - 180px);
  overflow: auto;
  margin-bottom: 15px;
}

.scrollbar-wrapper {
  height: 100%;
  overflow-x: hidden;
}

.app-footer {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  font-size: 13px;
  color: #909399;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.02);
}

.footer-links {
  display: flex;
  gap: 15px;
  
  a {
    color: #606266;
    text-decoration: none;
    transition: color 0.3s;
    
    &:hover {
      color: #4caf50;
    }
  }
}

@media (max-width: 992px) {
  .app-sidebar {
    position: absolute;
    height: calc(100vh - 60px);
    transform: translateX(-100%);
    transition: transform 0.3s;
    z-index: 1001;
  }
  
  .app-sidebar.is-collapse {
    transform: translateX(0);
    width: 220px;
  }
  
  .header-nav {
    display: none;
  }
  
  .app-main {
    padding: 10px;
  }
  
  .main-content {
    padding: 15px;
  }
  
  .app-footer {
    flex-direction: column;
    height: auto;
    padding: 10px;
    gap: 5px;
  }
}
</style>